Skip to content

fix: match literal MIME types with regex metacharacters in DocumentTypeRouter - #11973

Merged
anakin87 merged 3 commits into
deepset-ai:mainfrom
chuenchen309:fix/document-type-router-literal-mime
Jul 13, 2026
Merged

fix: match literal MIME types with regex metacharacters in DocumentTypeRouter#11973
anakin87 merged 3 commits into
deepset-ai:mainfrom
chuenchen309:fix/document-type-router-literal-mime

Conversation

@chuenchen309

Copy link
Copy Markdown
Contributor

Problem

DocumentTypeRouter compiles each declared MIME type as a regex and matches with
pattern.fullmatch(mime_type). A standard IANA type that contains a regex
metacharacter is therefore misinterpreted. The clearest case is the + in
image/svg+xml: as a regex, svg+xml means sv followed by one-or-more g
followed by xml, so an actual image/svg+xml document never matches and lands
in unclassified.

router = DocumentTypeRouter(mime_type_meta_field="mime_type", mime_types=["image/svg+xml"])
router.run(documents=[Document(content="<svg/>", meta={"mime_type": "image/svg+xml"})])
# -> {"unclassified": [...]}   (expected: {"image/svg+xml": [...]})

Fix

Match declared MIME types by exact equality first, then fall back to regex.
This routes literal types with metacharacters correctly while preserving regex
patterns such as audio/.*. The output bucket key is unchanged (it equals the
declared string, as before), so declared output types are unaffected. This
mirrors how FileTypeRouter handles the same situation.

Tests

Added test_run_with_literal_mime_type_containing_regex_metacharacter in
test/components/routers/test_document_type_router.py, asserting image/svg+xml
routes to its own bucket and a coexisting audio/.* regex pattern still
matches. Fails before the change, passes after. All 19 router tests pass.

$ python -m pytest test/components/routers/test_document_type_router.py
19 passed

ruff check / ruff format --check pass. Added a release note.


Disclosure: I used AI assistance (Claude) to find this regex-metacharacter
routing bug and draft the test. I reviewed the change, ran the suite, and take
responsibility for its correctness.

…peRouter

DocumentTypeRouter compiles each declared MIME type as a regex and matches with
`pattern.fullmatch`. A standard IANA type containing a regex metacharacter, most
notably the '+' in 'image/svg+xml', is thus misinterpreted: 'svg+xml' means
'svg' with one-or-more 'g', so 'image/svg+xml' documents never match and fall
into 'unclassified'.

Match declared MIME types by exact equality first, then fall back to regex, so
literal types with metacharacters route correctly while regex patterns like
'audio/.*' keep working. The output bucket key is unchanged (equal to the
declared string), so declared output types are unaffected.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@chuenchen309
chuenchen309 requested a review from a team as a code owner July 13, 2026 01:34
@chuenchen309
chuenchen309 requested review from anakin87 and removed request for a team July 13, 2026 01:34
@vercel

vercel Bot commented Jul 13, 2026

Copy link
Copy Markdown

@chuenchen309 is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@CLAassistant

CLAassistant commented Jul 13, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions Bot added the type:documentation Improvements on the docs label Jul 13, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/components/routers
  document_type_router.py
Project Total  

This report was generated by python-coverage-comment-action

@anakin87 anakin87 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@anakin87
anakin87 merged commit 91c4bc6 into deepset-ai:main Jul 13, 2026
22 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic:tests type:documentation Improvements on the docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants